<?php
/**
 * 日统计
 *
 * @author 孙晓晔
 * @version $Id: LogAction.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class TjDay extends AdminUtil {
	public $tbl = 'fy.tj_dd';

	public $orderby = array(
		'-' => '请选择',
		'dd' => '日期',
		'tbl' => '表',
		'type' => '类型',
		'1' => '创建时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'dd' => '日期',
		'tbl' => '表',
		'type' => '类型',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:29:07 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);

		$this->dsn = 'admin';
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('uri_cc', Common::cc());
	}

	function tab($controller, $request) {		
		$data = array(
			'日报表' => array('cc'=>'log.php?do=TjDay.find', 'js'=>''),
			'周报表' => array('cc'=>'log.php?do=TjWeek.find', 'js'=>''),
			'月报表' => array('cc'=>'log.php?do=TjMonth.find', 'js'=>''),
		
			'日统计' => array('cc'=>'log.php?do=TjDay.group', 'js'=>''),
			'周统计' => array('cc'=>'log.php?do=TjWeek.group', 'js'=>''),
			'月统计' => array('cc'=>'log.php?do=TjMonth.group', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	/**
	 * 首页
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function layout($controller, $request) {

		$controller->display($request, 'log/action');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
			
		$controller->assign('t1', strtotime('-1 day'));
		// $controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'log/dd/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {

		$tbl_s = $request['tbl_s'];
		$type_s = $request['type_s'];
		$dd1_s = $request['dd1_s'];
		$dd2_s = $request['dd2_s'];

		$where = " WHERE 1=1";
		if (!empty($tbl_s)) {
			$where .= " AND tbl = '" . $tbl_s . "'";
		}

		if (!empty($type_s)) {
			$where .= " AND type = '" . $type_s . "'";
		}

		if (!empty($dd1_s)) {
			$where .= " AND dd >= '" . $dd1_s . "'";
		}
		
		if (!empty($dd2_s)) {
			$where .= " AND dd <= '" . $dd2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['type_t'] = $this->_type($row['tbl'], $row['type']);
				$row['num_t'] = $this->_num($row['tbl'], $row['num']);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'log/dd/list');
		return true;
	}

	function _type($tbl, $type) {
		if ($tbl == "asteriskcdrdb.cdr") {
			return $this->_type_cdr($type);
		}

		return $type;
	}

	function _num($tbl, $num) {
		if ($tbl == "asteriskcdrdb.cdr") {
			return $this->_num_cdr($num);
		}

		return $num;
	}

	function _type_cdr($type) {
		$list = array(
		"1"=>"南山内部电话", 
		"2"=>"南山拔打沙井", 
		"3"=>"南山拔打市话", 
		"4"=>"南山拔打长途", 
		"5"=>"南山拔打网络", 
		"6"=>"南山拔打电话",
		
		"11"=>"沙井内部电话", 
		"12"=>"沙井拔打南山", 
		"13"=>"沙井拔打市话", 
		"14"=>"沙井拔打长途", 
		"15"=>"沙井拔打网络", 
		"16"=>"沙井拔打电话");

		return sprintf("%s[%s]", $list[$type], $type);
	}

	function _num_cdr($time) {
		$hour = floor($time / 3600);
		$minute = floor(($time % 3600) / 60);
		$second = $time % 60;

		return sprintf("%02d:%02d:%02d", $hour, $minute, $second);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		$controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'log/dd/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'dd');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
		
		$this->num_s = '2';

		$fmt = "SELECT %s AS a, SUM(num) AS c FROM %s a %s GROUP BY %s ORDER BY %s %s";
		$where = $this->_where($request);
		$request['sql'] = sprintf($fmt, $this->s, $this->tbl, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'dd') {
			return $row;
		}

		if ($px == 'type') {
			$row['a'] = $this->_type_cdr($row['a']);
		}
		
		$row = $this->_name_t($row, $px);

		return $row;
	}
	
	function _num_t($t) {
		return $this->printtime($t);
	}
	
	function printtime($time) {
		$hour = floor($time / 3600);
		$minute = floor(($time % 3600) / 60);
		$second = $time % 60;

		return sprintf("%02d:%02d:%02d", $hour, $minute, $second);
	}

	/**
	 * 定时运行
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'list.b@tecmold.com';

		$subject = '系统使用情况';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function _tj_a($controller, $request) {
		$this->pretreat($controller);

		$groupby_s = 'emp';
		$s = $this->groupby($controller, $groupby_s);
		$df = "DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')";

		$db = newdb($this->dsn_r);
		$sql = "SELECT $s AS a, ip, COUNT($s) AS c FROM " . $this->tbl . " a ";
		$where = " WHERE createtime > " . $df;

		$orderby = ' ORDER BY c DESC';

		$groupby = " GROUP BY $s, ip";
		$sql .= $where . $groupby . $orderby;
		debug($sql);
		$result = $db->query($sql);

		$data = array();
		$count = 0;
		$xx = 0;
		$total = 0;
		while ($row = $db->fetch_array($result)) {
			if ($row['a'] == '214') {
				continue;
			}

			$total += $row['c'];
			if ($count > 30) {
				$xx += $row['c'];
				continue;
			}

			if (empty($row['a'])) {
				continue;
			}

			$row = $this->_name($row, $groupby_s);

			$row['ip_t'] = $this->name_ip($row['ip']);

			$data[] = $row;
			$count++;
		}

		if ($xx > 0) {
			$data[] = array('a'=>'其它', 'c'=>$xx);
		}

		$aa = array();
		while (list($k, $v) = each($data)) {
			$v['p'] = number_format(($v['c'] / $total) * 100, 2, '.', '') . "%";
			$aa[] = $v;
		}
			
		$a = array();
		$c = array();
		reset($data);
		while (list($k, $v) = each($data)) {
			$a[] = $v['a'];
			$c[] = $v['c'];
		}
			
		$sa = join(",", $a);
		// echo "\n";
		$sc = join(",", $c);
			
		$sa = trim($sa, ",");
			
		$controller->assign('sa', $sa);
		$controller->assign('sc', $sc);

		$controller->assign_by_ref('total', $total);

		$controller->assign_by_ref('result', $aa);

		return $controller->sunxy($request, 'log/action/tj_a');
	}
}
?>
